/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.geekchique.runningbuddy.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.widget.Toast;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.activity.StopClock;
import com.geekchique.runningbuddy.controller.StopClockControllerImpl;

/**
 * This is an example of implementing an application service that runs locally
 * in the same process as the application.  The {@link LocalServiceController}
 * and {@link LocalServiceBinding} classes show how to interact with the
 * service.
 *
 * <p>Notice the use of the {@link NotificationManager} when interesting things
 * happen in the service.  This is generally how background services should
 * interact with the user, rather than doing something more disruptive such as
 * calling startActivity().
 */
public class TimeRunnerService extends Service
{
    private NotificationManager mNM;
	Intent intent;


	@Override
    protected void onCreate()
    {

		mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
        intent = new Intent();
        intent.setClass(this, StopClockControllerImpl.class);

        Toast.makeText(this, getText(R.string.local_service_started), 2);
//        mNM.notifyWithText(R.string.local_service_started, , NotificationManager.LENGTH_SHORT,   new Notification( R.drawable.stat_sample,  getText(R.string.local_service_label),  intent,   null,   null));
        
//        mNM.notify(2, new Notification( R.drawable.stat_sample,  getText(R.string.local_service_label)));
        setMood(R.drawable.stat_sample,  R.string.local_service_label, false);
     
    }	
	
    private void setMood(int moodId, int textId, boolean showTicker) {
        // This is who should be launched if the user selects our notification.
        Intent contentIntent = new Intent(this, StopClock.class);

        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(textId);

        // choose the ticker text
        String tickerText = showTicker ? getString(textId) : null;

        mNM.notify(
                   999999, // we use a string id because it is a unique
                                                      // number.  we use it later to cancel the
                                                      // notification
                   new Notification(
                       this,                        // our context
                       moodId,                      // the icon for the status bar
                       tickerText,                  // the text to display in the ticker
                       System.currentTimeMillis(),  // the timestamp for the notification
                       "Jog Started",
                                                    // the title for the notification
                       text,                        // the details to display in the notification
                       contentIntent,               // the contentIntent (see above)
                       R.drawable.stat_sample,  // the app icon
                       "Running Buddy", // the name of the app
                       intent));                 // the appIntent (see above)
    }
	
    @Override
    protected void onDestroy()
    {
    	mNM.cancel(R.string.local_service_started);

//        mNM.notifyWithText(R.string.local_service_stopped,
//                   getText(R.string.local_service_stopped),
//                   NotificationManager.LENGTH_SHORT,
//                   null);
        
    	Toast.makeText(this, getText(R.string.local_service_stopped), 2);
//      mNM.notifyWithText(R.string.local_service_started, , NotificationManager.LENGTH_SHORT,   new Notification( R.drawable.stat_sample,  getText(R.string.local_service_label),  intent,   null,   null));
      
      mNM.cancel(2);
      
    }


    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new Binder()
    {
        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
            return super.onTransact(code, data, reply, flags);
        }
    };


	@Override
	public IBinder onBind(Intent intent) {
        return mBinder;
	}
    
}

